package com.htfyun.ft.fragment.factory;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.htfyun.ft.R;
import com.htfyun.ft.config.FTBuildConfig;
import com.htfyun.ft.config.FTProperty;
import com.htfyun.ft.utils.UsefulUtils;
import com.htfyun.hardware.LedController;

import java.math.BigDecimal;

import androidx.annotation.Nullable;

public class FragmentRamRom extends FactoryBaseFragment {

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_factory_ram_rom;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);
        setBtnPassVisibility(View.GONE);
        TextView txtRamRomMinValue = (TextView) findViewById(R.id.txtRamRomMinValue);

        String ramRom = getString(R.string.ram_rom_min_val, getDefinedRamSize(), getDefinedRomSize());
        txtRamRomMinValue.setText(ramRom);
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        check();
    }

    private void check() {
        TextView txtRam = findViewById(R.id.txtRam);
        TextView txtRom = findViewById(R.id.txtRom);

        final ResultAttr ramResult = getRamResult();
        final ResultAttr romResult = getRomResult();

        txtRam.setText(UsefulUtils.getSpannableStringForResult(getContext(), getRamInfo(ramResult.getResult())));
        txtRom.setText(UsefulUtils.getSpannableStringForResult(getContext(), getRomInfo(romResult.getResult())));

        if (ramResult == ResultAttr.PASS && romResult == ResultAttr.PASS) {
            delayAutoPass();
        }
    }

    private String getRamInfo(String result) {
        return getString(R.string.ram_test_result, UsefulUtils.getRamNominalSize(), result);
    }

    private ResultAttr getRamResult() {
        BigDecimal definedRamSize = new BigDecimal(getDefinedRamSize());
        BigDecimal realRamSize = new BigDecimal(UsefulUtils.getRamTotalSize());
        ResultAttr result = realRamSize.compareTo(definedRamSize) > 0 ? ResultAttr.PASS : ResultAttr.FAIL;
        return result;
    }

    private String getRomInfo(String result) {
        return getString(R.string.rom_test_result, UsefulUtils.getRomNominalSize(), result);
    }

    private ResultAttr getRomResult() {
        BigDecimal definedRomSize = new BigDecimal(getDefinedRomSize());
        BigDecimal realRomSize = new BigDecimal(UsefulUtils.getRomTotalSize());
        ResultAttr result = realRomSize.compareTo(definedRomSize) > 0 ? ResultAttr.PASS : ResultAttr.FAIL;
        return result;
    }

    private float getDefinedRamSize() {
        float size = FTProperty.getRamSize();
        if (size < 0) {
            size = FTBuildConfig.RAM_SIZE_MIN;
        }

        return size;
    }

    private float getDefinedRomSize() {
        float size = FTProperty.getRomSize();
        if (size < 0) {
            size = FTBuildConfig.ROM_SIZE_MIN;
        }

        return size;
    }
}
